package cn.sxt.yxl.question.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.question.dao.IAColumnsDao;
import cn.sxt.yxl.question.pojo.AColumns;
import cn.sxt.yxl.question.service.IQuestionService;

@Service("questionService")
public class QuestionServiceImpl implements IQuestionService{
	@Resource
	private IAColumnsDao columnsDao;
	
	//查询一条栏目
	@Override
	public AColumns findOneColumnsService(Map<String, Object> condMap) {
		//将id变成整数类型的，传的时候什么类型都可以
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + "")){
			condMap.put("id", Integer.valueOf(condMap.get("id") + ""));
		}
		
		return this.columnsDao.findOne(condMap);
	}
	
	
	//保存一条记录
	@Override
	public JSONObject saveOneColumnsService(AColumns columns) {
		JSONObject resultJSON = new JSONObject();
		//保存记录
		int res = this.columnsDao.saveOne(columns);
		
		if(res > 0){
			//存储结果
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", columns.getId() + "");
			dataJSON.put("effectCount", res);

			resultJSON.put("data", dataJSON);

			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	
	
	//修改一条记录
	@Override
	public JSONObject updateOneColumnsService(AColumns columns) {
		JSONObject resultJSON = new JSONObject();
		//保存记录
		int res = this.columnsDao.updateOne(columns);
		
		if(res > 0){
			//存储结果
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", columns.getId() + "");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "失败");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	
	
	//删除一条记录
	@Override
	public JSONObject deleteOneColumnsService(AColumns columns) {
		JSONObject resultJSON = new JSONObject();
		//保存记录
		int res = this.columnsDao.deleteOne(columns);
		
		if(res > 0){
			/* 存储结果 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", columns.getId() + "");
			dataJSON.put("effectCount", res);

			resultJSON.put("data", dataJSON);

			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	
	
	//查询多条记录
	@Override
	public List<AColumns> findCondListColumnsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		
		if(pageInfoUtil != null){
			/*
			 * 分页
			 * 查询总记录数
			 */
			Page<AColumns> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			//查询数据
			List<AColumns>columnsList = this.columnsDao.findCondList(condMap);
			//设置总记录数，一定要在查询结果之后
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return columnsList;
		}
		return this.columnsDao.findCondList(condMap);
	}

}
